﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using Tesla.Content;
using Tesla.Graphics;

namespace Tesla.Scene {
    /// <summary>
    /// SceneHints is a collection of enumerations that are used to evaluate a Spatial in the scene graph. These properties
    /// can be inherited from the Spatial's parent, if the local hint is set to inherit.
    /// 
    /// </summary>
    public sealed class SceneHints : ISavable {

        private IHintable _source;
        private CullHint _cullHint;
        private PickingHint _pickHint;
        private LightCombineHint _lightHint;
        private TransparencyType _transHint;
        private RenderBucketType _bucketType;
        private int _orthoOrder;

        /// <summary>
        /// Gets or sets the TransparencyType. If the local is
        /// set to Inherit, then this is influenced by the parent hintable.
        /// </summary>
        public TransparencyType TransparencyType {
            get {
                //If not inherit, return local
                if(_transHint != TransparencyType.Default) {
                    return _transHint;
                }

                //If inherit, return parent's
                IHintable parent = _source.ParentHintable;
                if(parent != null) {
                    return parent.SceneHints.TransparencyType;
                }

                //If inherit with no parent, return default
                return TransparencyType.OneSided;
            }
            set {
                _transHint = value;
            }
        }

        /// <summary>
        /// Gets or sets the RenderBucketType. If the local is
        /// set to Inherit, then this is influenced by the parent hintable.
        /// </summary>
        public RenderBucketType RenderBucketType {
            get {
                //If not inherit, return local
                if(_bucketType != RenderBucketType.Default) {
                    return _bucketType;
                }

                //If inherit, return parent's
                IHintable parent = _source.ParentHintable;
                if(parent != null) {
                    return parent.SceneHints.RenderBucketType;
                }

                //If inherit with no parent, return default
                return RenderBucketType.Opaque;
            }
            set {
                _bucketType = value;
            }
        }

        /// <summary>
        /// Gets or sets the ortho order of the Spatial, this is used
        /// for when the spatial is to be drawn using the RenderBucketType.Ortho
        /// mode. The spatial's parent does not influence this.
        /// </summary>
        public int OrthoOrder {
            get {
                return _orthoOrder;
            }
            set {
                _orthoOrder = value;
            }
        }

        /// <summary>
        /// Gets or sets the CullHint. If the local hint is
        /// set to Inherit, then this is influenced by the parent hintable.
        /// </summary>
        public CullHint CullHint {
            get {
                //If not inherit, use local
                if(_cullHint != CullHint.Inherit) {
                    return _cullHint;
                }

                //If inherit, get the parent's
                IHintable parent = _source.ParentHintable;
                if(parent != null) {
                    return parent.SceneHints.CullHint;
                }

                //If inherit with no parent, use default
                return CullHint.Dynamic;
            }
            set {
                _cullHint = value;
            }
        }

        /// <summary>
        /// Gets or sets the LightCombineHint. If the local is
        /// set to Inherit, then this is influenced by the parent hintable.
        /// </summary>
        public LightCombineHint LightCombineHint {
            get {
                //If not inherit, return local
                if(_lightHint != LightCombineHint.Inherit) {
                    return _lightHint;
                }

                //If inherit, return parent's
                IHintable parent = _source.ParentHintable;
                if(parent != null) {
                    return parent.SceneHints.LightCombineHint;
                }

                //If inherit with no parent, return default
                return LightCombineHint.CombineClosest;
            }
            set {
                _lightHint = value;
            }
        }

        /// <summary>
        /// Gets or sets the PickingHint. If the local is
        /// set to Inherit, then this is influenced by the parent hintable.
        /// </summary>
        public PickingHint PickingHint {
            get {
                //If not inherit, return local
                if(_pickHint != PickingHint.Inherit) {
                    return _pickHint;
                }

                //If inherit, return parent's
                IHintable parent = _source.ParentHintable;
                if(parent != null) {
                    return parent.SceneHints.PickingHint;
                }

                //If inherit with no parent, return default
                return PickingHint.PickableAndCollidable;
            }
            set {
                _pickHint = value;
            }
        }

        /// <summary>
        /// Gets if the Spatial can be used in picking tests.
        /// </summary>
        public bool IsPickable {
            get {
                PickingHint pick = this.PickingHint;
                return pick == PickingHint.Pickable || pick == PickingHint.PickableAndCollidable;
            }
        }

        /// <summary>
        /// Gets if the Spatial can be used in collision tests.
        /// </summary>
        public bool IsCollidable {
            get {
                PickingHint pick = this.PickingHint;
                return pick == PickingHint.Collidable || pick == PickingHint.PickableAndCollidable;
            }
        }

        /// <summary>
        /// Sets hintable source.
        /// </summary>
        internal IHintable HintableSource {
            set {
                _source = value;
            }
        }

        //Do not use, for ISavable
        private SceneHints() {}

        /// <summary>
        /// Creates a new scene hints object with the specified source hintable.
        /// </summary>
        /// <param name="source">Source hintable</param>
        public SceneHints(IHintable source) {
            _source = source;
            _cullHint = CullHint.Inherit;
            _lightHint = LightCombineHint.Inherit;
            _transHint = TransparencyType.Default;
            _pickHint = PickingHint.Inherit;
            _orthoOrder = 0;
        }

        /// <summary>
        /// Serializes this SceneHints.
        /// </summary>
        /// <param name="output">Output to be written to</param>
        public void Write(ISavableWriter output) {
            output.WriteEnum<CullHint>("CullHint", _cullHint);
            output.WriteEnum<PickingHint>("PickingHint", _pickHint);
            output.WriteEnum<LightCombineHint>("LightCombineHint", _lightHint);
            output.WriteEnum<TransparencyType>("TransparencyType", _transHint);
            output.WriteEnum<RenderBucketType>("RenderBucketType", _bucketType);
            output.Write("OrthoOrder", _orthoOrder);
        }


        /// <summary>
        /// Deserializes this SceneHints.
        /// </summary>
        /// <param name="input">Input to read from</param>
        public void Read(ISavableReader input) {
            _cullHint = input.ReadEnum<CullHint>();
            _pickHint = input.ReadEnum<PickingHint>();
            _lightHint = input.ReadEnum<LightCombineHint>();
            _transHint = input.ReadEnum<TransparencyType>();
            _bucketType = input.ReadEnum<RenderBucketType>();
            _orthoOrder = input.ReadInt();
        }
    }
}
